package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.GWXQBean;
import com.example.administrator.szb.bean.YWBDNEWBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.Toasts;
import com.example.administrator.szb.xzyw.BGZRActivity;
import com.example.administrator.szb.xzyw.DXZFActivity;
import com.example.administrator.szb.xzyw.DZJCActivity;
import com.example.administrator.szb.xzyw.FHBSActivity;
import com.example.administrator.szb.xzyw.GGPZActivity;
import com.example.administrator.szb.xzyw.GPZYActivity;
import com.example.administrator.szb.xzyw.GQDRActivity;
import com.example.administrator.szb.xzyw.QYLCActivity;
import com.example.administrator.szb.xzyw.SBZRActivity;
import com.example.administrator.szb.xzyw.XYDKActivity;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by D N on 2017/11/9.
 */

public class XZGWYWActivity extends BaseActivity {
    @Bind(R.id.xzgwyw_recyclerview_yw)
    RecyclerView xzgwywRecyclerviewYw;
    @Bind(R.id.xzgwyw_button_xyb)
    Button xzgwywButtonXyb;
    public static YWBDNEWBean ywbdBean;
    int id;//点击下一步需要的id;
    String title;
    int counselor_id;
    int type = 2;
    int genre = 0;

    HashMap<Integer, Class> map = new HashMap<>();//保存跳转的layout.
    BaseAdapter<GWXQBean.DataBean.BusinessBean> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xzgwyw_activity);
        SampleApplicationLike.getActivitiesInstance().add(this);
        initToolbar(R.id.xzgwyw_toolbar_include, "选择业务");
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        xzgwywRecyclerviewYw.setLayoutManager(new GridLayoutManager(this, 3));
        initAdapter();
        xzgwywRecyclerviewYw.setAdapter(adapter);
    }

    private void initAdapter() {
        try{
            adapter = new BaseAdapter<GWXQBean.DataBean.BusinessBean>(this, (List<GWXQBean.DataBean.BusinessBean>)JGXQActivity.gwjgBean.getData().getBusiness()) {
                @Override
                public void convert(int position, BaseViewHolder holder, final GWXQBean.DataBean.BusinessBean businessBean) {
                    ((CheckBox) holder.getView(R.id.xzyw_radiobutton_gpzy)).setText(businessBean.getTitle());
                    if (businessBean.getIsChoose() == 0) {
                        ((CheckBox) holder.getView(R.id.xzyw_radiobutton_gpzy)).setChecked(false);
                    }
                    ((CheckBox) holder.getView(R.id.xzyw_radiobutton_gpzy)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            if (b) {
                                map.clear();
                                for (GWXQBean.DataBean.BusinessBean bean : (List<GWXQBean.DataBean.BusinessBean>)JGXQActivity.gwjgBean.getData().getBusiness()) {
                                    if (compoundButton.getText().toString().equals(bean.getTitle())) {
                                        bean.setIsChoose(1);
                                        id = businessBean.getId();
                                        if (bean.getTitle().equals("股票质押")) {
                                            map.put(bean.getId(), GPZYActivity.class);
                                        } else if (bean.getTitle().equals("大宗减持")) {
                                            map.put(bean.getId(), DZJCActivity.class);
                                        } else if (bean.getTitle().equals("企业理财")) {
                                            map.put(bean.getId(), QYLCActivity.class);
                                        } else if (bean.getTitle().equals("信用贷款")) {
                                            map.put(bean.getId(), XYDKActivity.class);
                                        } else if (bean.getTitle().equals("过桥短融")) {
                                            map.put(bean.getId(), GQDRActivity.class);
                                        } else if (bean.getTitle().equals("三板转让")) {
                                            map.put(bean.getId(), SBZRActivity.class);
                                        } else if (bean.getTitle().equals("并购转让")) {
                                            map.put(bean.getId(), BGZRActivity.class);
                                        } else if (bean.getTitle().equals("定向增发")) {
                                            map.put(bean.getId(), DXZFActivity.class);
                                        } else if (bean.getTitle().equals("杠杆配资")) {
                                            map.put(bean.getId(), GGPZActivity.class);
                                        } else {
                                            map.put(bean.getId(), FHBSActivity.class);
                                            title = bean.getTitle();
                                        }
                                    } else {
                                        bean.setIsChoose(0);
                                    }
                                }
                                adapter.notifyDataSetChanged();
                            }
                        }
                    });
                }

                @Override
                public int getLayoutId(int viewType) {
                    return R.layout.view_xzyw_item;
                }
            };

        }
        catch (Exception e){

        }

    }

    @OnClick(R.id.xzgwyw_button_xyb)
    public void onViewClicked() {
        try{
           // Class clazz = map.get(id);

                requestData();

        }catch (Exception e){
            Log.e("pdf", "onViewClicked: "+e.toString() );
        }
    }


    private void requestData() {
        DialogUtil.showIsoProgressbar(this,"加载数据中...");
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", id + "");
        map.put("counselor_id", counselor_id + "");
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        String url = URLAddress.URLBASE + URLAddress.URL_BDYW;
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, YWBDNEWBean.class, url, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ywbdBean = (YWBDNEWBean) response;
                DialogUtil.dialog_ios.dismiss();
                //mdata.addAll(ywbdBean.getData().getList());
                if (ywbdBean.getResult() == 1) {

                    Intent intent = new Intent(XZGWYWActivity.this,SQXMActivity.class);
                    intent.putExtra("title",title);
                    intent.putExtra("id",id);
                    for (int i = 0; i < SampleApplicationLike.getIndexBeanInstance().getData().getBusiness().size(); i++) {
                        if (SampleApplicationLike.getIndexBeanInstance().getData().getBusiness().get(i).getId() == id){
                            genre = SampleApplicationLike.getIndexBeanInstance().getData().getBusiness().get(i).getGenre();
                            type = SampleApplicationLike.getIndexBeanInstance().getData().getBusiness().get(i).getType();
                        }
                    }
                    intent.putExtra("type",type);
                    intent.putExtra("genre",genre);
                    intent.putExtra("counselor_id",getIntent().getIntExtra("counselor_id",-1));
                    startActivity(intent);
                    //initView();
                    // initRecyclerview();
                } else {
                    Toasts.show(XZGWYWActivity.this, "" + ywbdBean.getErr_msg(), Toast.LENGTH_SHORT);
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                DialogUtil.dialog_ios.dismiss();
                Toasts.show(XZGWYWActivity.this, "网络异常", Toast.LENGTH_SHORT);
            }
        });
    }
}
